const db = wx.cloud.database()
const _ = db.command
const col = "resources"
const sql = {
  _id: _.neq(1)
} //获取所有记录

Page({
  data: {
    // 无限查询
    list: [],
    limit: 20, //每次拉取数量
    clickList: [],
    item: 0,
    tab: 0,
  },

  // 页面切换
  changeItem: function (e) {
    this.setData({
      item: e.target.dataset.item,
    })
  },
  // tab切换
  changeTab: function (e) {
    this.setData({
      tab: e.detail.current
    })
  },
  onLoad: function () {
    this.getData()
    this.getClickData()
  },
  onCopy(e) {
    const { data, id, idx } = e.currentTarget.dataset
    let that = this
    db.collection(col).doc(id).update({
      data: {
        click: _.inc(1)
      },
      success: function (res) {
        let newData = that.data.list
        newData[idx].click += 1
        that.setData({
          list: newData
        })
      },
      fail: function (res) {
        console.log('点击自增失败:', res)
      }
    })
    wx.setClipboardData({
      data,
      success: () => {
        wx.getClipboardData({
          success: function (res) {
            wx.showToast({
              title: '复制成功'
            })
          }
        })
      }
    })
  },
  onClickCopy(e) {
    const { data, id, idx } = e.currentTarget.dataset
    let that = this
    db.collection(col).doc(id).update({
      data: {
        click: _.inc(1)
      },
      success: function (res) {
        let newData = that.data.clickList
        newData[idx].click += 1
        that.setData({
          clickList: newData
        })
      },
      fail: function (res) {
        console.log('点击自增失败:', res)
      }
    })
    wx.setClipboardData({
      data,
      success: () => {
        wx.getClipboardData({
          success: function (res) {
            wx.showToast({
              title: '复制成功'
            })
          }
        })
      }
    })
  },
  onShareAppMessage: function (res) {
    return {
      title: '阿里云盘福利码(扩容码)和资源！！！',
      path: '/pages/index/index',//这里是被分享的人点击进来之后的页面
      imageUrl: '/images/share.jpg'//这里是图片的路径
    }
  },
  getData: function () {
    db.collection(col)
      .where(sql)
      .orderBy('time', 'desc')
      .skip(this.data.list.length)
      .limit(this.data.limit)
      .get()
      .then(res => {
        this.setData({
          list: res.data
        })
      })
      .catch(err => {
        wx.showToast({ title: '加载失败', mask: true, icon: 'error' })
        console.error('getData()：', err)
      })
  },

  // 获取点击榜数据
  getClickData: function () {
    db.collection(col)
      .where(sql)
      .orderBy('click', 'desc')
      .limit(this.data.limit)
      .get()
      .then(res => {
        this.setData({
          clickList: res.data
        })
      })
      .catch(err => {
        wx.showToast({ title: '加载失败', mask: true, icon: 'error' })
        console.error('getClickData()：', err)
      })
  },
  // 告诉管理员资源失效
  handleLongPress(e) {
    wx.showModal({
      title: '提示',
      content: '是否告诉管理员该资源已失效？',
      // confirmText: '确认',
      success: ({ confirm }) => {
        if (confirm) {
          const { id } = e.currentTarget.dataset
          db.collection('resources').doc(id).update({
            data: {
              failure: true
            },
            success: function (res) {
              console.log(res)
            },
            fail: function (res) {
              console.log('资源失效:', res)
            }
          })
        }
      },
    })
  }
})